package middle;

import java.util.ArrayList;
import java.util.List;

public class Solution_46 {

    private boolean[] visited;
    private List<List<Integer>> list;

    public List<List<Integer>> permute(int[] nums) {
        visited = new boolean[nums.length];
        list = new ArrayList<>();
        dps(0, new ArrayList<>(), nums);
        return list;
    }

    private void dps(int index, List<Integer> rst, int[] nums) {
        if (rst.size() == nums.length) {
            list.add(new ArrayList<>(rst));
            return;
        }
        for (int i = index; i < nums.length; i++) {
            if (!visited[i]) {
                rst.add(nums[i]);
                visited[i] = true;
                dps(index, rst, nums);
                visited[i] = false;
                rst.remove(rst.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        Solution_46 model = new Solution_46();
        System.out.println(model.permute(new int[]{1, 2, 3}));
        System.out.println(model.permute(new int[]{0, 1}));
    }
}
